import * as types from '../mutations'
import * as auth from '../../api/auth.js'
import Vue from 'vue'

const adminKey = 'ADMIN';

// initial status
const state = {
  admin: JSON.parse(sessionStorage.getItem(adminKey))
}

// getters
const getters = {
  isLogin: state => state.admin !== null
}

// actions
const actions = {
  login({ commit }, admin) {
    commit(types.LOGIN, admin);
  },
  logout({ commit }) {
    commit(types.LOGOUT);
  }
}

// mutations
const mutations = {
  [types.LOGIN](state, admin) {
    sessionStorage.setItem(adminKey, JSON.stringify(admin));
    state.admin = admin;
  },
  [types.LOGOUT](state) {
    sessionStorage.removeItem(adminKey);
    state.admin = null;
  }
}

export default {
  state,
  getters,
  actions,
  mutations
}
